package com.engine.taskmask;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.engine.taskmask.persistence.PMF;
import com.engine.taskmask.persistence.iTask;
import com.engine.taskmask.sync.IDate;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class InitializeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user == null) {
			resp.sendRedirect("/index.jsp");
			return;
		}
		
		
		// test 
		
		
		iTask task1 = new iTask(new IDate(2012, 03, 12), "task1", "task1- do sth", "İzmir", "Test", true, true, user);
		iTask task2 = new iTask(new IDate(2012, 03, 12), "task2", "task2- do sth", "Ankara", "Yazılım", false, true, user);	
		iTask task3 = new iTask(new IDate(2012, 03, 12), "task3", "task3- do sth", "Bursa", "Uretim", true, true, user);
		iTask task4 = new iTask(new IDate(2012, 03, 12), "task4", "task4- do sth", "Istanbul", "Satis", true, false, user);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
        	pm.makePersistent(task1);
        	pm.makePersistent(task2);
        	pm.makePersistent(task3);
        	pm.makePersistent(task4);
        }finally {
            pm.close();
        }
		
		resp.sendRedirect("/index.jsp");
	}
}
